package com.xiaoyu.lingdian.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.xiaoyu.lingdian.controller.BaseController;
import com.xiaoyu.lingdian.core.mybatis.page.Page;
import com.xiaoyu.lingdian.entity.CoreAttachment;
import com.xiaoyu.lingdian.entity.CorePointsOrder;
import com.xiaoyu.lingdian.service.CoreAttachmentService;
import com.xiaoyu.lingdian.service.CorePointsOrderService;
import com.xiaoyu.lingdian.tool.out.ResultMessageBuilder;
import com.xiaoyu.lingdian.vo.CorePointsOrderVO;

@Controller
@RequestMapping(value="/corePointsOrder")
public class CorePointsOrderController extends BaseController {

	/**
	 * 商品订单表
	 */
	@Autowired
	private CorePointsOrderService corePointsOrderService;
	
	/**
	 * 附件表
	 */
	@Autowired
	private CoreAttachmentService coreAttachmentService;
		
	/**
	 *根据商品名称分页查询商品订单列表
	 * 
	 * @param crporGoodsName
	 * @param crporStatus
	 * @param pageNum
	 * @param pageSize
	 * @param response
	 */
	@RequestMapping(value = "/find/by/cnd", method = RequestMethod.POST)
	public void findByCnd(String crporGoodsName, Integer pageNum, Integer pageSize, HttpServletResponse response) {
		logger.info("[CorePointsOrderController.findByCnd]:begin findByCnd.");
		if (pageNum == null) {
			pageNum = 1;
		}
		if (pageSize == null) {
			pageSize = 10;
		}
		Page<CorePointsOrder> page = corePointsOrderService.findCorePointsOrderByCnd(crporGoodsName, null, pageNum, pageSize);
		Page<CorePointsOrderVO> pageVO = new Page<>(page.getPageNumber(), page.getPageSize(), page.getTotalCount());		
		List<CorePointsOrderVO> list = new ArrayList<>();
		CorePointsOrderVO vo = new CorePointsOrderVO();
		for (CorePointsOrder corePointsOrder : page.getResult()) {
			vo = new CorePointsOrderVO();
			vo.convertPOToVO(corePointsOrder);
			//根据配件商品UUID查询图片附件
			List<CoreAttachment> attachmentslist = coreAttachmentService.findCoreAttachmentByCnd(corePointsOrder.getCrporUuid());
			vo.setList(attachmentslist);
			list.add(vo);
		}
		pageVO.setResult(list);
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "查询商品订单成功", pageVO), response);
		logger.info("[CorePointsOrderController.findByCnd]:end findByCnd.");
	}

	/**
	 * 查询我的商品订单列表
	 * 
	 * @param crporUser
	 * @param response
	 */
	@RequestMapping(value = "/find/my/pointsorder", method = RequestMethod.POST)
	public void findMyPointsOrder(String crporUser, Integer pageNum, Integer pageSize, HttpServletResponse response) {
		logger.info("[CorePointsOrderController.findMyPointsOrder]:begin findMyPointsOrder.");
		if (pageNum == null) {
			pageNum = 1;
		}
		if (pageSize == null) {
			pageSize = 10;
		}
		if (StringUtils.isBlank(crporUser)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "缺少crporUser参数！"), response);
			return;
		}
		Page<CorePointsOrder> page = corePointsOrderService.findCorePointsOrderByCnd(null, crporUser, pageNum, pageSize);
		Page<CorePointsOrderVO> pageVO = new Page<>(page.getPageNumber(), page.getPageSize(), page.getTotalCount());		
		List<CorePointsOrderVO> list = new ArrayList<>();
		CorePointsOrderVO vo = new CorePointsOrderVO();
		for (CorePointsOrder corePointsOrder : page.getResult()) {
			vo = new CorePointsOrderVO();
			vo.convertPOToVO(corePointsOrder);
			//根据配件商品UUID查询图片附件
			List<CoreAttachment> attachmentslist = coreAttachmentService.findCoreAttachmentByCnd(corePointsOrder.getCrporUuid());
			vo.setList(attachmentslist);
			list.add(vo);
		}
		pageVO.setResult(list);
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "查询我的商品订单成功", pageVO), response);
		logger.info("[CorePointsOrderController.findMyPointsOrder]:end findMyPointsOrder.");
	}
	
	/**
	 * 修改商品订单（发货、已完成）、备注
	 * 
	 * @param crporUuid
	 * @param crporStatus
	 * @param crporRemarks
	 * @param response
	 */
	@RequestMapping(value="/update/pointsorder", method=RequestMethod.POST)
	public void updatePointsOrder(String crporUuid, Integer crporStatus, String crporRemarks, HttpServletResponse response) {
		logger.info("[CorePointsOrderController.updatePointsOrder]:begin updatePointsOrder");
		if (StringUtils.isBlank(crporUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "缺少crporUuid参数！"), response);
			return;
		}
		if (null == crporStatus || 0 == crporStatus) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请输入商品订单状态！"), response);
			return;
		}
		CorePointsOrder corePointsOrder = new CorePointsOrder();
		corePointsOrder.setCrporUuid(crporUuid);
		corePointsOrder.setCrporStatus(crporStatus);
		corePointsOrder.setCrporRemarks(crporRemarks);
		corePointsOrderService.updateCorePointsOrder(corePointsOrder);
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "更新商品订单成功！"), response);
		logger.info("[CorePointsOrderController.updatePointsOrder]:end updatePointsOrder");
	}
	
	/**
	 * 查看商品订单
	 * 
	 * @param crporUuid
	 * @param response
	 */
	@RequestMapping(value="/views", method=RequestMethod.POST)
	public void viewsPointsOrder(String crporUuid, HttpServletResponse response) {
		logger.info("[CorePointsOrderController.viewsPointsOrder]:begin viewsPointsOrder");
		if (StringUtils.isBlank(crporUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "缺少crporUuid参数！"), response);
			return;
		}
		CorePointsOrder corePointsOrder = new CorePointsOrder();
		corePointsOrder.setCrporUuid(crporUuid);
		corePointsOrder = corePointsOrderService.getCorePointsOrder(corePointsOrder);
		CorePointsOrderVO vo = new CorePointsOrderVO();
		vo.convertPOToVO(corePointsOrder);
		//根据配件商品UUID查询图片附件
		List<CoreAttachment> attachmentslist = coreAttachmentService.findCoreAttachmentByCnd(corePointsOrder.getCrporUuid());
		vo.setList(attachmentslist);
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "查看商品订单详情成功！", vo), response);
		logger.info("[CorePointsOrderController.viewsPointsOrder]:end viewsPointsOrder");
	}
	
	/**
	 * 删除商品订单（彻底删除）
	 * 
	 * @param crporUuid
	 * @param response
	 */
	@RequestMapping(value="/delete/one", method=RequestMethod.POST)
	public void deleteOne(String crporUuid, HttpServletResponse response) {
		logger.info("[CorePointsOrderController.deleteOne]:begin deleteOne");
		if (StringUtils.isBlank(crporUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "缺少crporUuid参数！"), response);
			return;
		}
		CorePointsOrder corePointsOrder = new CorePointsOrder();
		corePointsOrder.setCrporUuid(crporUuid);
		corePointsOrderService.deleteCorePointsOrder(corePointsOrder);
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "删除商品订单成功！"), response);
		logger.info("[CorePointsOrderController.deleteOne]:end deleteOne");
	}
	
	/**
	 * 批量删除商品订单
	 * 
	 * @param crporUuids
	 * @param response
	 */
	@RequestMapping(value="/delete/batch", method=RequestMethod.POST)
	public void deleteBatch(String crporUuids, HttpServletResponse response) {
		logger.info("[CorePointsOrderController.deleteBatch]:begin deleteBatch");
		if (StringUtils.isBlank(crporUuids)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请选择批量删除对象！"), response);
			return;
		}
		String[] array = crporUuids.split("\\|");
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < array.length; i++) {
			list.add(array[i]);
		}
		if (list.size() == 0) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请选择批量删除对象！"), response);
			return;
		}
		corePointsOrderService.deleteCorePointsOrderByCnd(list);
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "删除商品订单成功！"), response);
		logger.info("[CorePointsOrderController.deleteBatch]:end deleteBatch");
	}
		
}